﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Utils;
using Payroll.Common.Utils;
using PayrollHR.Web;
using Payroll.BusinessLogic.EmployeeManagement;


namespace PayrollHR.Administration.Modules
{
    public partial class EmployeeAdd : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindEmployee();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    lblEmployeeExists.Visible = false;
                    save();
                    Response.Redirect("ListEmployee.aspx");
                }
                catch (Exception exc)
                {
                    lblEmployeeExists.Visible = true;
                    ProcessException(exc);
                }
            }
        }
        private void BindEmployee()
        {
            if (this.EmployeeID == 0)
            {
                return;
            }
            var _employee = this.EmployeeService.GetEmployeeById(this.EmployeeID);
            if (CommonHelper.IsNull(_employee))
            {
                return;
            }
            ctrlEmployeeInfo.bindData(_employee);
        }
        private void save()
        {
           
            var _employee = ctrlEmployeeInfo.getEmployee();
            if (_employee.Name == "" | _employee.Username == "")
            {
                return;
            }
            if (this.EmployeeID == 0)
            {
               
                this.EmployeeService.SaveEmployee(_employee);
            }
            else
            {
                _employee.EmployeeId = this.EmployeeID;
                this.EmployeeService.UpdateEmployee(_employee);
            }
        }

        public int EmployeeID
        {
            get
            {
                return CommonHelper.QueryStringInt("Id");
            }
        }
        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            save();
        }
    }
}